package com.hundsun.yr.universal.library.view.gallery;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hundsun.yr.universal.library.adapter.ViewCreator;



/**
 * @Title: HSGalleryPagerAdapter.java 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @date 2015-12-14 上午11:27:21 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class HSGalleryPagerAdapter<T> extends PagerAdapter{


  
    private final List<T> data;
  
    
    private final SparseArray<View> liveViews;
    // adapter destroyItem 时存放view
    private Stack<View> viewStack;
    
    /**
     * 用于从XML文件中创建Layout
     */
    private LayoutInflater mInflater;
    /**
     * View创建器
     */
    private ViewCreator<T> mCreator;
    
    public HSGalleryPagerAdapter(Context mContext ,ViewCreator<T> creator) {
        this(mContext, creator, null);
    }
    
    public HSGalleryPagerAdapter(Context mContext, ViewCreator<T> creator, List<T> data) {
        this.mCreator = creator;
        this.mInflater=LayoutInflater.from(mContext);
        this.data = data==null ? new ArrayList<T>() : new ArrayList<T>(data);
        this.liveViews = new SparseArray<View>();
        viewStack = new Stack<View>();
    }

    public T getItem(int position) {
        if (position >= getCount()){
            return null;
        } 
        return data.get(position);
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        viewStack.push((View)object);
        liveViews.remove(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        
        View view = mCreator.createView(mInflater,(ViewGroup) container, position, getItem(position));
		
		mCreator.updateView(view,position, getItem(position));
		
		
		((ViewPager) container).addView(view, 0);
        VersionDiffUtils.scaleY(view, HSGallery.GALLEY_SCALE_RATE);
        VersionDiffUtils.scaleX(view, HSGallery.GALLEY_SCALE_RATE);
//        VersionDiffUtils.setAlpha(view, 1f);
        liveViews.put(position, view);
        return view;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
    	
    	if(liveViews.get(position-1)!=null){
    		if(liveViews.get(position-1).getScaleX()>0.93){
    			VersionDiffUtils.scaleY(liveViews.get(position-1), 0.9f);
    			VersionDiffUtils.scaleX(liveViews.get(position-1), 0.9f);
    		}
    	}
    	if(liveViews.get(position+1)!=null){
    		if(liveViews.get(position+1).getScaleX()>0.93){
    			VersionDiffUtils.scaleY(liveViews.get(position+1), 0.9f);
    			VersionDiffUtils.scaleX(liveViews.get(position+1), 0.9f);
    		}
    	}
    	
        VersionDiffUtils.scaleY(liveViews.get(position), 1f);
        VersionDiffUtils.scaleX(liveViews.get(position), 1f);
        super.setPrimaryItem(container, position, object);
    }
    
    public View getLiveView(int position) {
        return liveViews == null||liveViews.size() > 0 ? liveViews.get(position) : null;
    }
    
    public void add(T elem) {
        data.add(elem);
        notifyDataSetChanged();
    }

    public void add(int index,T elem) {
        data.add(index,elem);
        notifyDataSetChanged();
    }
    
    public void addAll(List<T> elem) {
        data.addAll(elem);
        notifyDataSetChanged();
    }

    public List<T> getAdapterList(){
        return data;
    }
    
    public void set(T oldElem, T newElem) {
        set(data.indexOf(oldElem), newElem);
    }

    public void set(int index, T elem) {
        data.set(index, elem);
        notifyDataSetChanged();
    }

    public void remove(T elem) {
        data.remove(elem);
        notifyDataSetChanged();
    }

    public void removeAll(List<T> elems){
        data.removeAll(elems);
        notifyDataSetChanged();
    }
    
    public int indexOfItem(T elem){
        return data.indexOf(elem);
    }
    
    public void remove(int index) {
        data.remove(index);
        notifyDataSetChanged();
    }

    public boolean contains(T elem) {
        return data.contains(elem);
    }

    /** Clear data list */
    public void clear() {
        data.clear();
        notifyDataSetChanged();
    }
    
}
